package Config::System::File;

use Moose;
use Config::Std;

has path   => (is => 'ro', isa => 'Str', required => 1);
has parsed => (is => 'rw', isa => 'HashRef');

sub parse {
    my $self = shift;
    return %{ $self->parsed } if $self->parsed;
    my %parsed = eval { $self->_parse };
    die "Can't load configuration file [". $self->path. "]: $@" if $@;
    $self->parsed(\%parsed);
    return %parsed;
}

sub _parse {
    my $self = shift;
    read_config $self->path => my %config;
    my %parsed;
    for my $section_name (keys %config) {
        $self->validate_section_name($section_name);
        $parsed{$section_name} = $config{$section_name};
    }
    return %parsed;
}

sub validate_section_name {
    my ($self, $name) = @_;
    die "No support for global section yet" if $name eq '';
    die "Section name must be valid Perl identifier, [$name] is not" unless $name =~ /^[_a-zA-Z][\w:]*$/;
}

sub clone {
    my $self = shift;
    return ref($self)->new(path => $self->path);
}

1;
#
__END__

a configuration file
parsed using Config::Std
